package com.xianniu.ltc.controller;

import cn.devezhao.commons.web.ServletUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONAware;
import com.alibaba.fastjson.JSONObject;
import com.rebuild.api.RespBody;
import com.rebuild.core.UserContextHolder;
import com.rebuild.utils.JSONUtils;
import com.rebuild.web.BaseController;
import com.rebuild.web.user.signup.LoginController;
import com.xianniu.ltc.common.Constant;
import com.xianniu.ltc.model.po.AddXnLimitRulePO;
import com.xianniu.ltc.model.po.UpdateXnLimitRulePO;
import com.xianniu.ltc.model.vo.XnLimitRuleFallbackVO;
import com.xianniu.ltc.model.vo.XnLimitRuleVO;
import com.xianniu.ltc.service.XnLimitRuleService;
import com.xxl.sso.core.entity.ReturnT;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author WangRuiJie
 * @Title: XnLimitRuleController
 * @Package com.xianniu.ltc.controller
 * @Description: 限制规则查询
 * @date 2021/6/154:47 下午
 */
@RestController
@Slf4j
@RequestMapping(value = "/limitRule")
public class XnLimitRuleController extends BaseController {

    @Autowired
    private XnLimitRuleService ruleService;

    /***********************************************领取数量限制*********************************************/
    /**
     * 后台规则：查询领取数量限制
     * @return
     */
    @RequestMapping("/selectCountRule/{entity}")
    public ReturnT selectCountRule(@PathVariable String entity, HttpServletRequest request) {
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        List<XnLimitRuleVO> voList = ruleService.selectCountRule(entity);
        JSONObject json = new JSONObject();
        json.put("voList", voList);
        if (CollectionUtils.isNotEmpty(voList)){
            json.put("isDisable", voList.get(0).getIsDisable());
        }else {
            json.put("isDisable", 1);
        }
        return new ReturnT(json);
    }

    /**
     * 领取数量限制开关设置
     * @param entity
     * @param request
     * @return
     */
    @RequestMapping("/isDisabledCountRule/{entity}")
    public JSONAware isDisabledCountRule(@PathVariable String entity, HttpServletRequest request){
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        ReturnT returnT = ruleService.isDisabledCountRule(entity);
        if (returnT.getCode() != 200){
            return RespBody.errorl(returnT.getMsg());
        }
        return RespBody.ok();
    }

    /**
     * 领取数量限制开关设置
     * @param request
     * @return
     */
    @RequestMapping("/deleteCountRule")
    public JSONAware deleteCountRule(Integer id, HttpServletRequest request){
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        ReturnT returnT = ruleService.deleteCountRule(id);
        if (returnT.getCode() != 200){
            return RespBody.errorl(returnT.getMsg());
        }
        return RespBody.ok();
    }

    /**
     * 新增领取数量限制规则
     * @param request
     * @return
     */
    @PostMapping("/addLeadCountRule")
    public JSONAware addLeadCountRule(@RequestBody AddXnLimitRulePO po, HttpServletRequest request){
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        log.info("新增领取数量限制规则：{}", JSON.toJSON(po));
        ReturnT returnT = ruleService.addLeadCountRule(po);
        if (returnT.getCode() != 200){
            return RespBody.errorl(returnT.getMsg());
        }
        return RespBody.ok();

    }

    /**
     * 修改领取数量限制规则
     * @param request
     * @return
     */
    @PostMapping("/updateLeadCountRule")
    public JSONAware updateLeadCountRule(@RequestBody UpdateXnLimitRulePO po, HttpServletRequest request){
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        log.info("修改领取数量限制规则：{}", JSON.toJSON(po));
        ReturnT returnT = ruleService.updateRule(po);
        if (returnT.getCode() != 200){
            return RespBody.errorl(returnT.getMsg());
        }
        return RespBody.ok();

    }


    /***********************************************线索领取限制*********************************************/
    /**
     * 后台规则：查询线索领取规则
     * @return
     */
    @RequestMapping("/selectReceiveRule/{entity}")
    public ReturnT selectReceiveRule(@PathVariable String entity,HttpServletRequest request) {
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        XnLimitRuleVO vo = ruleService.selectReceiveRule(entity);
        return new ReturnT(vo);
    }

    /**
     * 修改线索领取规则
     * @param request
     * @return
     */
    @PostMapping("/updateReceiveRule")
    public JSONAware updateReceiveRule(@RequestBody UpdateXnLimitRulePO po, HttpServletRequest request){
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        log.info("修改线索领取规则：{}", JSON.toJSON(po));
        ReturnT returnT = ruleService.updateRule(po);
        if (returnT.getCode() != 200){
            return RespBody.errorl(returnT.getMsg());
        }
        return RespBody.ok();

    }


    /***********************************************线索回收限制*********************************************/
    /**
     * 后台规则：查询领取数量限制
     * @return
     */
    @RequestMapping("/selectFallbackRule/{entity}")
    public ReturnT selectFallbackRule(@PathVariable String entity, HttpServletRequest request) {
        JSONObject json = new JSONObject();
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        XnLimitRuleFallbackVO vo = ruleService.selectFallbackRule(entity);
        json.put("list", vo.getFallbackVoList());
        json.put("isDisable", vo.getIsDisable());
        json.put("againRule", vo.getAgainRule());
        json.put("lockRule", vo.getLockRule());
        return new ReturnT(json);
    }

    /**
     * 自动回收开关设置
     * @param entity
     * @param request
     * @return
     */
    @RequestMapping("/isDisabledFallback/{entity}")
    public JSONAware isDisabledFallback(@PathVariable String entity, HttpServletRequest request){
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        ReturnT returnT = ruleService.isDisabledFallback(entity);
        if (returnT.getCode() != 200){
            return RespBody.errorl(returnT.getMsg());
        }
        return RespBody.ok();
    }

    /**
     * 修改回收规则
     * @param request
     * @return
     */
    @PostMapping("/updateFallbackRule")
    public JSONAware updateFallbackRule(@RequestBody UpdateXnLimitRulePO po, HttpServletRequest request){
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        log.info("修改回收规则：{}", JSON.toJSON(po));
        ReturnT returnT = ruleService.updateRule(po);
        if (returnT.getCode() != 200){
            return RespBody.errorl(returnT.getMsg());
        }
        return RespBody.ok();

    }

    /**
     * 修改再次领取规则
     * @param request
     * @return
     */
    @RequestMapping("/updateAgainRule/{entity}")
    public JSONAware updateAgainRule(@PathVariable String entity, Integer value, HttpServletRequest request){
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        ReturnT returnT = ruleService.updateAgainRule(entity, value);
        if (returnT.getCode() != 200){
            return RespBody.errorl(returnT.getMsg());
        }
        return RespBody.ok();

    }

    /**
     * 修改客户锁规则
     * @param request
     * @return
     */
    @RequestMapping("/updateAccountLockRule")
    public JSONAware updateAccountLockRule(Integer value, HttpServletRequest request){
        String tenantCode = (String) ServletUtils.getSessionAttribute(request, LoginController.TENANT_CODE);
        UserContextHolder.setTenant(tenantCode);
        ReturnT returnT = ruleService.updateAccountLockRule(value);
        if (returnT.getCode() != 200){
            return RespBody.errorl(returnT.getMsg());
        }
        return RespBody.ok();

    }

}
